package com.t235.gc.goods.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.t235.gc.AjaxResult;
import com.t235.gc.entity.GoodsCategory;
import com.t235.gc.goods.service.GoodsCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * 商品分类表(GoodsCategory)表控制层
 *
 * @author makejava
 * @since 2021-03-24 08:45:32
 */
@Slf4j
@RestController
@RequestMapping("/api/goods-category")
public class GoodsCategoryController {
    @Autowired
    private GoodsCategoryService goodsService;

    /**
     * 商品分类
     * @param goodsCategoryName
     * @param currPage
     * @param pageSize
     * @return
     */
    @GetMapping("/admin/getBackGoodsCategoryList")
    public AjaxResult getBackGoodsCategoryList(String goodsCategoryName, @RequestParam(value = "page") Integer currPage, @RequestParam("limit") Integer pageSize){
        PageHelper.startPage(currPage,pageSize);
        List<GoodsCategory> list=goodsService.getGoodsList(goodsCategoryName);
        PageInfo<GoodsCategory> pageInfo = new PageInfo<>(list);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 新增商品分类
     * @param goodsCategory
     * @return
     */
    @PostMapping("/admin/saveGoodsCategory")
    public AjaxResult saveGoodsCategory(GoodsCategory goodsCategory){
        Random random=new Random();
        Long index= (long)random.nextInt(99999999);//随机数
        goodsCategory.setGoodscategoryid(index);
        goodsCategory.setCname("周安");
        int num=goodsService.add(goodsCategory);
        if (num>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改商品分类
     * @param goodsCategory
     * @return
     */
    @PostMapping("/admin/UpdateGoodsCategory")
    public AjaxResult UpdateGoodsCategory(GoodsCategory goodsCategory){
        int num=goodsService.update(goodsCategory);
        if (num>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 删除商品分类
     * @param goodsCategoryId
     * @return
     */
    @GetMapping("/admin/delGoodsCategory")
    public AjaxResult delGoodsCategory(Long goodsCategoryId){
        int num=goodsService.del(goodsCategoryId);
        if (num>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
